//
//  PreferentialLeftViewController.m
//  Project_A Gary
//
//  Created by leilei on 16/4/1.
//  Copyright © 2016年 www.lei.com. All rights reserved.
//

#import "PreferentialLeftViewController.h"
#import "PreferentialRightViewController.h"
#import "PreferentialLeftModel.h"
#import "PreLeftDetailViewController.h"
#import "PreferentialLeftTableViewCell.h"


@interface PreferentialLeftViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain)UITableView *tableV;
@property (nonatomic, retain)NSMutableArray *dataArr;
// 设置一个属性判断是否切换
@property (nonatomic, assign)BOOL isChange;



@end

@implementation PreferentialLeftViewController
-(void)dealloc
{
    [_tableV release];
    [_dataArr release];
    
    [super dealloc];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.navigationItem.title = @"车辆列表";
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:32 / 255.0 green:105 / 255.0 blue:236 / 255.0 alpha:1];
    // 建立个按钮 放在导航栏的右上角
    UIBarButtonItem *change = [[UIBarButtonItem alloc] initWithTitle:@"特惠活动" style:UIBarButtonItemStylePlain target:self action:@selector(change)];
    self.navigationItem.rightBarButtonItem = change;
    
    [self createTableView];
    [self dataHandle];

}

#pragma mark - 切换界面按钮方法(通过动画)
- (void)change
{
//     使用单例的方法
    PreferentialRightViewController *rightVC = [PreferentialRightViewController singleInstance];
    
    if (!_isChange) {
        [UIView transitionFromView:self.view toView:rightVC.view duration:2 options:UIViewAnimationOptionTransitionCurlUp completion:^(BOOL finished) {
            
        }];
    } else {
        
        [UIView transitionFromView:rightVC.view toView:self.view duration:2 options:UIViewAnimationOptionTransitionCurlDown completion:^(BOOL finished) {
        }];
    }
    
    // 把这个BOOL值取反
    self.isChange = !self.isChange;
    
//    PreferentialRightViewController *rightVC  = [[PreferentialRightViewController alloc]init];
//    [self.navigationController pushViewController:rightVC animated:YES];
    
}

#pragma mark - tableView 两个必须写方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    PreferentialLeftTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"PreferentialLeftTableViewCell" forIndexPath:indexPath];
    PreferentialLeftModel *preModel = self.dataArr[indexPath.row];
    
    
    cell.backgroundColor = [UIColor colorWithRed:250 / 255.0 green:250 / 255.0 blue:250 / 255.0 alpha:1];
    cell.dealerName.text = preModel.dealerName;
    cell.dealerPrice.text = preModel.dealerPrice;
    cell.modelName.text = preModel.modelName;
    
    
    [cell.phoneBtn addTarget:self action:@selector(phoneClick:) forControlEvents:UIControlEventTouchUpInside];
    
    // 设置phoneBtn按钮的tag值 为了后面能得到这个indexpath.row
    cell.phoneBtn.tag = 3000 + indexPath.row;
    
    
    [cell.preferentialBtn addTarget:self action:@selector(preferentialClick:) forControlEvents:UIControlEventTouchUpInside];
    
    // 设置phoneBtn按钮的tag值 为了后面能得到这个indexpath.row
    cell.preferentialBtn.tag = 4000 + indexPath.row;
    
    return cell;
}


#pragma mark - tableView 点击方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    PreLeftDetailViewController *detailVC = [[PreLeftDetailViewController alloc] init];
    
    PreferentialLeftModel *leftModel = self.dataArr[indexPath.row];
    detailVC.modelId = leftModel.modelId;
    
    [self.navigationController pushViewController:detailVC animated:YES];
}

#pragma mark - 电话咨询 按钮点击方法
- (void)phoneClick: (UIButton *)phoneBtn
{
    // 建立个integer数值 然后通过tag值取到indexPath.row  再通过model取值
    NSInteger i = phoneBtn.tag - 3000;
    PreferentialLeftModel *preModel = self.dataArr[i];
    
    //添加标题
    UIAlertController *pc =  [UIAlertController alertControllerWithTitle:@"咨询电话" message:preModel.phone preferredStyle:UIAlertControllerStyleAlert];
    
    // 添加按钮
    [pc addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
    }]];
    [pc addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
    }]];
    // 在alert上面添加text输入框
    [pc addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
        
    }];
    // 模态跳转
    [self presentViewController:pc animated:YES completion:^{
        
    }];

    
}



#pragma mark - 优惠 按钮点击方法
- (void)preferentialClick: (UIButton *)preBtn
{
    // 建立个integer数值 然后通过tag值取到indexPath.row  再通过model取值
    NSInteger i = preBtn.tag - 4000;
    PreferentialLeftModel *leftModel = self.dataArr[i];
    
    // 建立alert
    UIAlertController *preC = [UIAlertController alertControllerWithTitle:@"优惠后:" message:[NSString stringWithFormat:@"%@万(元)", leftModel.modelPrice] preferredStyle:UIAlertControllerStyleAlert];
    
    // 添加按钮
    [preC addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
    }]];
    // 添加按钮
    [preC addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
    }]];
    
    // 模态跳转
    [self presentViewController:preC animated:YES completion:^{
        
    }];
    
    
}



#pragma mark - 数据解析
- (void)dataHandle
{
    NSString *str = @"http://mrobot.pcauto.com.cn/v3/price/promotionList?orderBy=popularity&pageSize=20&pageNo=2&areaId=248";
    
    [NetworkHandle GetDataWithURL:str dic:nil success:^(id responseObject) {
        
        self.dataArr = [PreferentialLeftModel transformWithArray:responseObject[@"data"]];
        [self.tableV reloadData];
        
    } filed:^(NSError *error) {
        
    }];
}


#pragma mark - 创建tableView
- (void)createTableView
{
    self.tableV = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, BOUNDS_WIDTH, BOUNDS_HEIGHT) style:UITableViewStylePlain];
    self.tableV.backgroundColor = [UIColor whiteColor];
    self.tableV.delegate = self;
    self.tableV.dataSource = self;
    self.tableV.rowHeight = 160;
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    [self.view addSubview:self.tableV];
    
    [self.tableV registerClass:[PreferentialLeftTableViewCell class] forCellReuseIdentifier:@"PreferentialLeftTableViewCell"];
    [_tableV release];
    
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
